using UnityEngine;

public class BoundaryLimit : MonoBehaviour
{
    private float screenLeft;
    private float screenRight;
    private float screenTop;
    private float screenBottom;
    
    void Start()
    {
        Camera mainCamera = Camera.main;
        screenLeft = mainCamera!.ScreenToWorldPoint(Vector3.zero).x;
        screenRight = mainCamera.ScreenToWorldPoint(new Vector3(Screen.width, 0, 0)).x;
        screenTop = mainCamera.ScreenToWorldPoint(new Vector3(0, Screen.height, 0)).y;
        screenBottom = mainCamera.ScreenToWorldPoint(Vector3.zero).y;
    }
    
    void Update()
    {
        Vector3 currentPosition = transform.position;
        
        currentPosition.x = Mathf.Clamp(currentPosition.x, screenLeft, screenRight);
        currentPosition.y = Mathf.Clamp(currentPosition.y, screenBottom, screenTop);
        
        transform.position = currentPosition;
    }
}
